package com.senjoeson.socialsecure.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.senjoeson.socialsecure.R;
import com.senjoeson.socialsecure.adapter.HomeAdapter;
import com.senjoeson.socialsecure.utils.SpeechUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener, HomeAdapter.OnSpeakFunctionListener {

    private static final String TAG = "MainActivity";
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    public TextView mToolBarTitle;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.tab_one)
    TextView mTabOne;
    @BindView(R.id.tab_two)
    TextView mTabTwo;
    @BindView(R.id.tab_three)
    TextView mTabThree;

    public Unbinder mUnbinder;
    public HomeAdapter mHomeAdapter;
    public SpeechUtil mSpeechUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        mUnbinder = ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        mToolBarTitle = mToolbar.findViewById(R.id.toolbar_title);
        mToolBarTitle.setText("首页");
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mRecyclerView.setNestedScrollingEnabled(false);
        mHomeAdapter = new HomeAdapter();
        mRecyclerView.setAdapter(mHomeAdapter);

        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabThree.setOnClickListener(this);

        mTabOne.setOnLongClickListener(this);
        mTabTwo.setOnLongClickListener(this);
        mTabThree.setOnLongClickListener(this);
        mHomeAdapter.setOnSpeakTextListener(this);


        mSpeechUtil = new SpeechUtil(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, ListActivity.class);

        switch (v.getId()) {
            case R.id.tab_one:
                //  intent.putExtra("url","http://new.sysb.gov.cn/pages/1/1xq/news_a00bc579fd2b48f283e9eaa25cb0d366.html");
                //intent.putExtra("url","http://www.sysb.gov.cn/index.html");
                intent.putExtra("title", "养老保险资讯");
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/home/more_1.html");
                break;
            case R.id.tab_two:
                //intent.putExtra("url","http://new.sysb.gov.cn/pages/2/2xq/news_c64ebe4d363a449a904a4aa66babc0b3.html");
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/home/more_2.html");
                intent.putExtra("title", "政务公开");
                break;
            case R.id.tab_three:
                // intent.putExtra("url","http://new.sysb.gov.cn/pages/8/8xq/news_27a8c28d9a1e4fee992ec32554a0bac8.html");
                intent.putExtra("title", "三公开");
                intent.putExtra("loadDataType", 1);
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/19/more_19.html");
                break;
        }
        startActivity(intent);
    }


    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.tab_one:
                mSpeechUtil.speak("养老保险资讯");

                break;
            case R.id.tab_two:
                mSpeechUtil.speak("政务公开");
                break;
            case R.id.tab_three:
                mSpeechUtil.speak("三公开");
                break;
        }
        return true;
    }




    @Override
    protected void onDestroy() {
        if (mSpeechUtil != null) {
            mSpeechUtil.stop();
            mSpeechUtil.release();
            mSpeechUtil = null;
        }
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        super.onDestroy();
    }


    @Override
    public void speak(String text) {
        mSpeechUtil.speak(text);
    }
}




